package com.yhn.init.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhn.init.common.BaseResponse;
import com.yhn.init.common.DeleteRequest;
import com.yhn.init.common.ResultUtils;
import com.yhn.init.model.dto.approvals.ApprovalsAddRequest;
import com.yhn.init.model.dto.approvals.ApprovalsPurchaseRequest;
import com.yhn.init.model.dto.approvals.ApprovalsQueryRequest;
import com.yhn.init.model.dto.approvals.ApprovalsRequest;
import com.yhn.init.model.vo.ApprovalsVO;
import com.yhn.init.service.ApprovalsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 审批信息
 *
 * @author yhnyhn123
 * @email 2236751002@qq.com
 * @date 2024-03-08 20:36:42
 */
@RestController
@RequestMapping("/approvals")
public class ApprovalsController {

    @Resource
    private ApprovalsService approvalsService;

    /**
     * 列表,分页
     */
    @GetMapping("/list")
    //@SaCheckPermissions("furniture/approvals/list")
    public BaseResponse<Page<ApprovalsVO>> listByPage(@RequestBody ApprovalsQueryRequest approvalsQueryRequest) {
        Page<ApprovalsVO> page = approvalsService.queryPage(approvalsQueryRequest);
        return ResultUtils.success(page);
    }


    /**
     * 获取单个信息
     */
    @GetMapping("/info/{id}")
    //@SaCheckPermissions("furniture/approvals/info")
    public BaseResponse<ApprovalsVO> getInfoById(@PathVariable("id") Long id) {
        ApprovalsVO approvals = approvalsService.getInfoById(id);

        return ResultUtils.success(approvals);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    // @SaCheckPermissions("furniture/approvals/save")
    public BaseResponse<Long> save(@RequestBody ApprovalsAddRequest approvalsAddRequest) {
        Long newId = approvalsService.saveApprovals(approvalsAddRequest);

        return ResultUtils.success(newId);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@SaCheckPermissions("furniture/approvals/update")
    public BaseResponse<Boolean> update(@RequestBody ApprovalsRequest approvalsUpdateRequest) {
        boolean result = approvalsService.updateApprovals(approvalsUpdateRequest);

        return ResultUtils.success(result);
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    //@SaCheckPermissions("furniture/approvals/delete")
    public BaseResponse<Boolean> delete(@RequestBody DeleteRequest deleteRequest) {
        boolean result = approvalsService.deleteById(deleteRequest);
        return ResultUtils.success(result);
    }

    /**
     * 对订单进行审批
     *
     * @param approveRequest 审批请求
     * @return 审批结果
     */
    @PostMapping("/order")
    @SaCheckPermission(value = {"/furniture/order/examine", "root"}, mode = SaMode.OR)
    public BaseResponse<Boolean> approvals(@RequestBody @Valid ApprovalsRequest approveRequest) {
        return ResultUtils.success(approvalsService.approvals(approveRequest));
    }

    /**
     * 对采购清单进行审批
     *
     * @param approvalsPurchaseRequest 审批请求
     * @return 审批结果
     */
    @PostMapping("/purchase")
    @SaCheckPermission(value = {"/furniture/purchase/examine", "root"}, mode = SaMode.OR)
    public BaseResponse<Boolean> approvalPurchase(@RequestBody @Valid ApprovalsPurchaseRequest approvalsPurchaseRequest) {
        return ResultUtils.success(approvalsService.approvalPurchase(approvalsPurchaseRequest));
    }
}
